package com.shop.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shop.entity.UserInfo;
@WebServlet(name="login",urlPatterns="/login")
public class LoginServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.添加属性
		request.setAttribute("username", "张无忌");
		request.setAttribute("password", "123456");
		
		//2.更新属性
		request.setAttribute("username", "赵敏");
		
		//3.删除属性
		request.removeAttribute("password");
		
		//获取登录信息
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		UserInfo user = new UserInfo();
		user.setUsername(username);
		user.setPassword(password);
		//存储到session
		HttpSession session = request.getSession();
		session.setAttribute("u", user);//触发HttpSessionAttributeListener
		request.getRequestDispatcher("/login.jsp").forward(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
}
